//package com.tbvtc.agriculturalcreditchainserver2025.service.impl;
//
//
//
//import com.tbvtc.agriculturalcreditchainserver2025.service.ClimateService;
//import org.geotools.data.DataStoreFinder;
//import org.geotools.data.shapefile.ShapefileDataStore;
//import org.geotools.data.simple.SimpleFeatureCollection;
//import org.geotools.data.simple.SimpleFeatureSource;
//import org.geotools.feature.FeatureIterator;
//import org.locationtech.jts.geom.Coordinate;
//import org.locationtech.jts.geom.GeometryFactory;
//import org.locationtech.jts.geom.Point;
//import org.opengis.feature.simple.SimpleFeature;
//import org.springframework.stereotype.Component;
//import org.springframework.stereotype.Service;
//
//import java.io.File;
//import java.io.IOException;
//import java.nio.charset.Charset;
//import java.util.HashMap;
//import java.util.Map;
//@Service
//public class PreciseChinaClimateService implements ClimateService {
//
//    private SimpleFeatureCollection climateFeatures;
//    private final GeometryFactory geometryFactory = new GeometryFactory();
//    private final String climateTypeAttributeName = "CliTypeCHN"; // 确保与shp字段名一致
//
//    public PreciseChinaClimateService(String shapefilePath) throws IOException {
//        File file = new File(shapefilePath);
//        Map<String, Object> map = new HashMap<>();
//        map.put("url", file.toURI().toURL());
//
//        // 显式使用ShapefileDataStore并设置编码
//        ShapefileDataStore dataStore = (ShapefileDataStore) DataStoreFinder.getDataStore(map);
//        dataStore.setCharset(Charset.forName("GBK"));  // 根据实际数据编码设置，可能是GBK或UTF-8
//
//        try {
//            String typeName = dataStore.getTypeNames()[0];
//            SimpleFeatureSource source = dataStore.getFeatureSource(typeName);
//            this.climateFeatures = source.getFeatures();
//        } finally {
//            dataStore.dispose();
//        }
//    }
//
//    @Override
//    public String getClimateType(double latitude, double longitude) {
//        Point queryPoint = geometryFactory.createPoint(new Coordinate(longitude, latitude));
//
//        try (FeatureIterator<SimpleFeature> iterator = climateFeatures.features()) {
//            while (iterator.hasNext()) {
//                SimpleFeature feature = iterator.next();
//                if (feature.getDefaultGeometry() instanceof org.locationtech.jts.geom.Geometry) {
//                    org.locationtech.jts.geom.Geometry geometry = (org.locationtech.jts.geom.Geometry) feature.getDefaultGeometry();
//                    if (geometry.contains(queryPoint)) {
//                        String climateType = (String) feature.getAttribute(climateTypeAttributeName);
//                        return climateType != null ? climateType : "未知气候类型";
//                    }
//                }
//            }
//        }
//        return "未知气候类型";
//    }
//
//
//
//
//
//
//
//
//
//
//    public static void main(String[] args) {
//        String shapefilePath = "F:\\fisco\\_2025AgriculturalCreditChainServer\\src\\main\\resources\\气候区划\\气候区划.shp"; // 实际路径
//
//        try {
//            PreciseChinaClimateService service = new PreciseChinaClimateService(shapefilePath);
//
//            // 测试点应包含已知结果的位置
//            System.out.println("北京: " + service.getClimateType(39.906217, 116.3912757)); // 应返回"暖温带大陆性季风气候"
//            System.out.println("海口: " + service.getClimateType(20.0, 110.3)); // 应返回"热带季风气候"
//            System.out.println("肯尼亚: " + service.getClimateType(-3.8, 39.3)); // 应返回"热带季风气候"
//            System.out.println("埃塞俄比亚: " + service.getClimateType(10.5, 40.3)); // 应返回"热带季风气候"
//            System.out.println("冰岛: " + service.getClimateType(64.1, -15.3)); // 应返回"温带大陆性季风气候"
//            System.out.println("缅甸: " + service.getClimateType(21.9, 95.9)); // 应返回"温带大陆性季风气候"
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//}